package com.nowcoder.community.controller;

import com.nowcoder.community.annotation.LoginRequired;
import com.nowcoder.community.entity.Comment;
import com.nowcoder.community.entity.DiscussPost;
import com.nowcoder.community.entity.Event;
import com.nowcoder.community.entity.User;
import com.nowcoder.community.event.EventProducer;
import com.nowcoder.community.service.CommentService;
import com.nowcoder.community.service.DiscussPostService;
import com.nowcoder.community.util.CommunityConstant;
import com.nowcoder.community.util.HostHolder;
import com.nowcoder.community.util.RedisKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;

@Controller
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private EventProducer eventProducer;

    @Autowired
    private CommentService commentService;

    @Autowired
    private DiscussPostService discussPostService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @RequestMapping(value = "/add/{discussPostId}", method = RequestMethod.POST)
    @LoginRequired
    public String addComment(Comment comment, @PathVariable("discussPostId") int discussPostId) {
        User loginUser = hostHolder.getUser();
        comment.setUserId(loginUser.getId());
        comment.setStatus(0);
        comment.setCreateTime(new Date());
        commentService.addComment(comment);

        // 确定评论或回复的目标用户
        int entityUserId = 0;
        if (comment.getEntityType() == CommunityConstant.ENTITY_TYPE_POST) {
            DiscussPost target = discussPostService.findDiscussPostById(comment.getEntityId());
            entityUserId = target.getUserId();
        } else if (comment.getEntityType() == CommunityConstant.ENTITY_TYPE_COMMENT) {
            if (comment.getTargetId() != 0) {
                entityUserId = comment.getTargetId();
            } else {
                Comment target = commentService.findCommentById(comment.getEntityId());
                entityUserId = target.getUserId();
            }
        }
        // 当前登录用户和目标用户不一致才触发通知事件
        if (loginUser.getId() != entityUserId) {
            Event event = new Event()
                    .setTopic(CommunityConstant.TOPIC_COMMENT)
                    .setUserId(loginUser.getId())
                    .setEntityType(comment.getEntityType())
                    .setEntityId(comment.getEntityId())
                    .setEntityUserId(entityUserId)
                    .setData("postId", discussPostId);
            eventProducer.produceEvent(event);
        }

        if (comment.getEntityType() == CommunityConstant.ENTITY_TYPE_POST) {
            // 触发发帖子事件，用于更新es中的帖子
            Event event = new Event()
                    .setTopic(CommunityConstant.TOPIC_PUBLISH)
                    .setUserId(loginUser.getId())
                    .setEntityType(CommunityConstant.ENTITY_TYPE_POST)
                    .setEntityId(discussPostId);
            eventProducer.produceEvent(event);

            // 将postId存入redis，用于定时任务更新帖子score
            String postScoreKey = RedisKeyUtil.getPostScoreKey();
            redisTemplate.opsForSet().add(postScoreKey, discussPostId);
        }

        return "redirect:/discuss/detail/" + discussPostId;
    }

}
